package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.project.flowable.service.ActProcessTaskService;
import com.dhecp.project.system.domain.SysModuleTask;
import com.dhecp.project.system.service.SysModuleNoticeService;
import com.dhecp.project.system.service.SysModuleTaskService;

/**
 * 功能任务 控制层
 * @author DH-Xxj
 * @version 2020年4月24日
 */
@Controller
@RequestMapping("/system/moduletask")
public class SysModuleTaskController extends BaseController{

	private String prefix = "system/moduletask";
    private String moduleTable = ModuleTable.MODULE_TASK;
    
    @Autowired
    private SysModuleTaskService moduleTaskService;
    
    @Autowired
    private SysModuleNoticeService moduleNoticeService;
    
    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;
    
    @Autowired
    private ActProcessTaskService actProcessTaskService;
    
    /**
     * 打开功能任务页面
     * @param params
     * @return
     * @author DH-Xxj
     */
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysModuleTask()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 100);
    	
		ModelAndView mv = this.getModelAndView();
		
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName(prefix + "/moduletask");
		return mv;
    }
    
    /**
     * 查询生成功能任务表格gridColumns
     * @param params
     * @return
     */
    @GetMapping("/grid")
    @ResponseBody
    public Map<String, Object> selectGrid(@RequestParam Map<String, Object> params){
    	Map<String, Object> dicFieldsList = new HashMap<String, Object>();
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysModuleTask()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 100);
    	dicFieldsList=gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
		return dicFieldsList;
    }

    /**
     * 打开功能任务新增页面
     * @param params
     * @return
     */
    @GetMapping("/add")
    public ModelAndView noticeListPage(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysModuleTask()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
		Map<String, Object> gridColumns = new HashMap<String, Object>();
		gridColumns = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
		
		ModelAndView mv = this.getModelAndView();
		mv.addObject("gridColumns", gridColumns);
		mv.setViewName(prefix + "/notice");
		return mv;
    }
	
	/**
     * 新增功能任务
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @PostMapping("/addModuleTask")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult moduleTaskAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= moduleTaskService.insertModuleTask(params);
			
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
	
	/**
	 * 查询未处理任务数
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listwcls")
    @ResponseBody
    public TableDataInfo listWcls(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        long actCount = 0;		//流程任务数
		Integer taskCount = 0;	//功能任务数
		Integer planCount = 0;	//计划数量
		Integer finishCount = 0;//总结数量
        List<Map<String, Object>> taskCountList =  new ArrayList<Map<String, Object>>();
        try {
        	actCount = actProcessTaskService.queryCurrentUserTodoActRuntimeTaskCount(0);
        	taskCount = moduleTaskService.queryModuleTaskWcls(params);
        	planCount = moduleTaskService.queryModuleTaskPlans(params);
        	finishCount = moduleTaskService.queryModuleTaskFinishs(params);
        	Map<String, Object> taskCountMap = new HashMap<String, Object>();
        	taskCountMap.put("actCount", actCount);
        	taskCountMap.put("taskCount", taskCount);
        	taskCountMap.put("planCount", planCount);
        	taskCountMap.put("finishCount", finishCount);
        	taskCountList.add(taskCountMap);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(taskCountList);
    }
	
	/**
	 * 查询未处理任务
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listwcl")
    @ResponseBody
    public TableDataInfo listWcl(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        List<SysModuleTask> moduleTaskList = new ArrayList<SysModuleTask>();
        try {
        	moduleTaskList = moduleTaskService.queryModuleTaskWcl(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(moduleTaskList);
    }
	
	/**
	 * 查询已处理任务
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listycl")
    @ResponseBody
    public TableDataInfo listYcl(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        List<SysModuleTask> moduleTaskList = new ArrayList<SysModuleTask>();
        try {
        	moduleTaskList = moduleTaskService.queryModuleTaskYcl(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(moduleTaskList);
    }
	
	/**
	 * 查询当前用户为发送人数据
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listyfs")
    @ResponseBody
    public TableDataInfo listFs(@RequestParam Map<String, Object> params) {
		params.put("moduleTable", moduleTable);
        List<SysModuleTask> moduleTaskList = new ArrayList<SysModuleTask>();
        try {
        	moduleTaskList = moduleTaskService.queryModuleTaskFs(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(moduleTaskList);
    }
    
    /**
     * 修改功能任务状态
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @PostMapping("/editTaskState")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult TaskStateEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= moduleTaskService.updateTaskState(params);
			
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 修改功能任务状态并生成通知
     * @param guid 必填参数
	 * @param taskState 必填参数：保存时可省略，提交时为（taskTaskState = "已处理"）
	 * @param moduleTable 功能唯一标识
     * @return AjaxResult
     * @author lwl
     */
    @PostMapping("/reply")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult Reply(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= moduleTaskService.reply(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
	
}
